<?php

namespace App\Http\Controllers\F;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\FinanceAccount;
use App\Models\FinanceRecord;
use Illuminate\Support\Facades\DB;

class BalanceController extends Controller
{
    
    public function index(Request $request)
    {
        if($request->input("name")){
            $title = $request->input("name");
            $arr = DB::table("users")->where("name","like","%$title%")->select("id")->first();
            if($arr){
                $uid = $arr->id;
            }else{
                $uid = null;
            }
            $data = FinanceAccount::orderBy("total","desc")->orderBy("id","desc")->where("user_id",$uid)->paginate(10);
        }else{
            $data = FinanceAccount::orderBy("total","desc")->orderBy("id","desc")->paginate(10);
        }
      if($data){
        foreach($data as $v){
            
            if(getUserName($v['user_id'])){
                $a = getUserName($v['user_id']);
                $v['name'] = $a->name;
               
            }else{
                $v['name'] = "";
            }
        }
    }

        return view("balance.index",['list'=>$data]);
    }
    public function detail(Request $request){
        $id = $request->input("id");
        $data = FinanceRecord::where("user_id",$id)->orderBy("id","desc")->paginate(10);
        foreach($data as $v){
                    if(getUserName($v['user_id'])){
                        $a = getUserName($v['user_id']);
                        $v['name'] = $a->name;
                       
                    }else{
                        $v['name'] = "";
                    }
                    $v['courseName'] = get_course_name1($v['course_class_id']);
            }
           
        return view("balance.detail",['list'=>$data,'id'=>$id]);
    }

  
   
   
}
